package com.xujl.mvcbind.ui.db.model;

import com.xujl.fastlib.base.BaseModel;
import com.xujl.fastlib.base.IControl;
import com.xujl.fastlib.utils.NumberUtils;
import com.xujl.mvcbind.App;
import com.xujl.mvcbind.bean.OverTimeEntity;
import com.xujl.mvcbind.bean.OverTimePageEntity;
import com.xujl.mvcbind.db.DBManager;
import com.xujl.mvcbind.db.OverTimeHelper;
import com.xujl.task.Emitter;
import com.xujl.task.RxExecutor;
import com.xujl.task.Task;

import java.util.ArrayList;
import java.util.List;


/**
 * <pre>
 *     author : xujl
 *     e-mail : 597355068@qq.com
 *     time   : 2019/08/20
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class OverTimeFragmentModel extends BaseModel {
    private OverTimeHelper mHelper;
    private OverTimePageEntity mPageEntity = new OverTimePageEntity();
    private List<OverTimeEntity> mDataList;

    @Override
    public void init (IControl control) {
        mDataList = new ArrayList<>();
    }

    public void initDb () {
        DBManager.getInstance().initGreenDao(App.getApplication());
        mHelper = DBManager.getInstance().getOverTimeHelper();
    }

    public List<OverTimeEntity> getDataList () {
        return mDataList;
    }

    public void countTotal () {
        RxExecutor.getInstance()
                .executeTask(new Task<Object>() {
                    @Override
                    public void run (Emitter<Object> emitter) throws Exception {
                        super.run(emitter);
                        final List<OverTimeEntity> overTimeList = mHelper.queryAllForType(OverTimeEntity.TYPE_OVERTIME);
                        final List<OverTimeEntity> leaveList = mHelper.queryAllForType(OverTimeEntity.TYPE_LEAVE);
                        double overTime = 0;
                        double leaveTime = 0;
                        for (OverTimeEntity entity : overTimeList) {
                            overTime += NumberUtils.doubleOf(entity.getDuration());
                        }
                        for (OverTimeEntity entity : leaveList) {
                            leaveTime += NumberUtils.doubleOf(entity.getDuration());
                        }
                        mPageEntity.setOverTimeTotal(overTime + " H");
                        mPageEntity.setLeaveTotal(leaveTime + " H");
                        mPageEntity.setResidueTotal((overTime - leaveTime) + " H");
                    }
                });
    }

    public void searchList (final int page, final int pageSize, final OnDataResult callback) {
        RxExecutor.getInstance()
                .executeTask(new Task<List<OverTimeEntity>>() {
                    @Override
                    public void run (Emitter<List<OverTimeEntity>> emitter) throws Exception {
                        super.run(emitter);
                        emitter.next(mHelper.queryAll(page, pageSize));
                    }

                    @Override
                    public void onNext (List<OverTimeEntity> data) {
                        super.onNext(data);
                        callback.dataCallback(data);
                    }
                });
    }

    public OverTimePageEntity getPageEntity () {
        return mPageEntity;
    }

    public void deleteEntity (int position) {
        mHelper.delete(mDataList.get(position));
    }

    public interface OnDataResult {
        void dataCallback (List<OverTimeEntity> list);
    }
}
